﻿using DevExpress.XtraEditors;
using EkStudio.com.bean.xml.BillModule;
using EkStudio.com.bean.xml.ProjectMenu;
using EkStudio.com.bean.xml.reflection.JAXB;
using EkStudio.com.bean.xml.RootInterface.Field;
using EkStudio.com.bean.xml.Unit;
using EkStudio.com.bean.xml.WorkFlow;
using EkStudio.com.common.unit;
using EkStudio.com.ui.unitbill.property.Event;
using System;
using System.Xml;

namespace EkStudio.com.ui.form
{
    public partial class FlowTargetModuleForm : XtraForm
    {

        private log4net.ILog logger = log4net.LogManager.GetLogger(typeof(FlowTargetModuleForm));

        private bool rOnly = false;
        private WflowModuleItem module = null;
        private ProjectRootPanel RootPanel = null;

        private delegate void UpdateStatusDelegate(string key, string xml);
        public FlowTargetModuleForm(ProjectRootPanel Root,WflowModuleItem bean,bool ReadOnly)
        {
            InitializeComponent();
            rOnly = ReadOnly;
            module = bean;
            RootPanel = Root;
            initForm();
        }

        private void initForm()
        {
            ModuleComboBox.ReadOnly = rOnly;
            KeyComboBox.ReadOnly = rOnly;
            StatusComboBox.ReadOnly = rOnly;
            RemarkComboBox.ReadOnly = rOnly;

            if (module.Workflow.FlowField == null) module.Workflow.FlowField = new WflowFieldElement();

            bean.xml.ProjectMenu.ProjectMenuCommonElement index = null;
            #region 加载模块列表
            if (RootPanel != null && RootPanel.MenuItem != null && RootPanel.MenuItem.Business != null)
            {
                
                bean.xml.ProjectMenu.ProjectMenuModRoot ModRoot = RootPanel.MenuItem.Business;
                if (ModRoot.Modules != null && ModRoot.Modules.Count > 0)
                {
                    foreach(bean.xml.ProjectMenu.ProjectMenuModGroup gp in ModRoot.Modules)
                    {
                        if (gp.Module != null && gp.Module.Count > 0)
                        {
                            foreach(bean.xml.ProjectMenu.ProjectMenuCommonElement el in gp.Module)
                            {
                                ModuleComboBox.Properties.Items.Add(el);
                                if(index==null&&module.Workflow.FlowField.Key == el.Command)
                                {
                                    index = el;
                                }
                            }
                        }
                    }
                }
            }
            #endregion

            if (index != null)
            {
                ModuleComboBox.SelectedItem = index;
                //异步加载模块信息
                bool ReadOnly = false;
                RootPanel.Http.AsyncResourcesXmlBean(index.Command, index.Caption, ProjectUtil.TYPE_BILL_MODULE, ref ReadOnly, null, LoadXml);
                LoadProgress.Visible = true;
            }
        }

        /// <summary>
        /// 加载服务器异步回调方法转界面线程 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoadXml(object sender, PropertyDataEvent e)
        {
            //异步转同步
            this.BeginInvoke(new UpdateStatusDelegate(LoadXmlToThreed), new object[] { e.Key, (string)e.Value });

        }


        /// <summary>
        /// xml转bean
        /// </summary>
        /// <param name="key"></param>
        /// <param name="xml"></param>
        private void LoadXmlToThreed(string key, string xml)
        {
            LoadProgress.Visible = false;
            BillModuleItem loadBill = null;
            KeyComboBox.Properties.Items.Clear();KeyComboBox.SelectedItem = null;
            StatusComboBox.Properties.Items.Clear(); StatusComboBox.SelectedItem = null;
            RemarkComboBox.Properties.Items.Clear(); RemarkComboBox.SelectedItem = null;

            if (key == "OK")
            {
                XmlDocument XmlDoc = new XmlDocument();
                try
                {
                    XmlDoc.LoadXml(xml);
                    JAXBContext<BillModuleItem> jaxb = new JAXBContext<BillModuleItem>();
                    try
                    {
                        loadBill = jaxb.unmarshal(XmlDoc.OuterXml);
                    }
                    catch (Exception ex)
                    {
                        logger.Error("XMLTOBean失败", ex);
                        loadBill = null;
                    }

                }
                catch (Exception ex)
                {
                    logger.Error("返回Xml格式化失败:" + ex.Message, ex);
                    loadBill = null;
                }
            }
            if(loadBill != null)
            {
                //加载字段
                if (loadBill.UiField != null && loadBill.UiField.FieldsProperties != null)
                {
                    foreach(BillUiGroupItems group in loadBill.UiField.FieldsProperties)
                    {
                        if (!group.Table&&group.Fields!=null&&group.Fields.Count>0)
                        {
                            foreach(BillUiFieldsItem field in group.Fields)
                            {
                                if (field.DataProperty.DataSource == "") continue;

                                KeyComboBox.Properties.Items.Add(field);
                                if (field.Key == module.Workflow.FlowField.Field)
                                {
                                    KeyComboBox.SelectedItem = field;
                                }

                                StatusComboBox.Properties.Items.Add(field);
                                if (field.Key == module.Workflow.FlowField.Status)
                                {
                                    StatusComboBox.SelectedItem = field;
                                }

                                if (field.DataProperty.DataType == BeanUtil.KT_DTMEMO) {
                                    RemarkComboBox.Properties.Items.Add(field);
                                    if (field.Key == module.Workflow.FlowField.Remark)
                                    {
                                        RemarkComboBox.SelectedItem = field;
                                    }
                                }
                                
                                
                                
                            }
                        }
                    }
                }
            }
        }

        private void ModuleComboBox_EditValueChanged(object sender, EventArgs e)
        {
            if(ModuleComboBox.SelectedItem is ProjectMenuCommonElement)
            {
                ProjectMenuCommonElement index = ModuleComboBox.SelectedItem as ProjectMenuCommonElement;
                bool ReadOnly = false;
                RootPanel.Http.AsyncResourcesXmlBean(index.Command, index.Caption, ProjectUtil.TYPE_BILL_MODULE, ref ReadOnly, null, LoadXml);
                LoadProgress.Visible = true;
            }
        }

        private void EnteryButton_Click(object sender, EventArgs e)
        {
            if (!rOnly)
            {
                ProjectMenuCommonElement index = ModuleComboBox.SelectedItem as ProjectMenuCommonElement;
                if(index!=null)
                {
                    module.Workflow.FlowField.Caption = index.Caption;
                    module.Workflow.FlowField.Key = index.Command;
                }
                else
                {
                    module.Workflow.FlowField.Caption = "";
                    module.Workflow.FlowField.Key = "";
                }
                BillUiFieldsItem field = KeyComboBox.SelectedItem as BillUiFieldsItem;
                if (field != null)
                {
                    module.Workflow.FlowField.Field = field.Key;
                    module.Workflow.FlowField.FieldCaption = field.Caption;
                    module.Workflow.FlowField.FieldSource = field.DataProperty.DataSource;
                }
                else
                {
                    module.Workflow.FlowField.Field = "";
                    module.Workflow.FlowField.FieldCaption = "";
                    module.Workflow.FlowField.FieldSource = "";
                }

                field = StatusComboBox.SelectedItem as BillUiFieldsItem;
                if(field != null)
                {
                    module.Workflow.FlowField.Status = field.Key;
                    module.Workflow.FlowField.StatusCaption = field.Caption;
                    module.Workflow.FlowField.StatusSource = field.DataProperty.DataSource;
                }
                else
                {
                    module.Workflow.FlowField.Status = "";
                    module.Workflow.FlowField.StatusCaption = "";
                    module.Workflow.FlowField.StatusSource = "";
                }

                field = RemarkComboBox.SelectedItem as BillUiFieldsItem;
                if (field != null)
                {
                    module.Workflow.FlowField.Remark = field.Key;
                    module.Workflow.FlowField.RemarkCaption = field.Caption;
                    module.Workflow.FlowField.RemarkSouce = field.DataProperty.DataSource;
                }
                else
                {
                    module.Workflow.FlowField.Remark = "";
                    module.Workflow.FlowField.RemarkCaption = "";
                    module.Workflow.FlowField.RemarkSouce = "";
                }
            }
            else
            {
                DialogResult = System.Windows.Forms.DialogResult.Cancel;
            }
        }
    }
}